package Controller;

import entity.Cake;
import entity.Cart;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

public class ViewCartServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");

        out.println("<html><head><title>购物车</title></head><body>");
        out.println("<h1>我的购物车</h1>");

        if (cart == null || cart.isEmpty()) {
            out.println("<p>购物车是空的</p>");
        } else {
            out.println("<table border='1'>");
            out.println("<tr><th>蛋糕名称</th><th>价格</th></tr>");
            for (Cake item : cart.getItems()) {
                out.println("<tr>");
                out.println("<td>" + item.getName() + "</td>");
                out.println("<td>￥" + item.getPrice() + "</td>");
                out.println("</tr>");
            }
            out.println("</table>");
        }

        out.println("<div style='margin-top: 20px;'>");
        out.println("<a href='CakeListServlet'>继续购物</a>");
        out.println("</div></body></html>");
    }
}
